package com.ruoyi.orderEat.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.orderEat.domain.ShopFlavour;
import com.ruoyi.orderEat.domain.ShopFlavourDTO;
import com.ruoyi.orderEat.domain.ShopFlavourVO;
import com.ruoyi.orderEat.service.IShopFlavourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 商品口味Controller
 *
 * @author DingHao
 * @date 2024-01-12
 */
@RestController
@RequestMapping("/orderEat/flavour")
public class ShopFlavourController extends BaseController
{
    @Autowired
    private IShopFlavourService shopFlavourService;

    /**
     * 查询商品口味列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShopFlavourDTO shopFlavourDTO)
    {
        startPage();
        List<ShopFlavourVO> list = shopFlavourService.selectShopFlavourList(shopFlavourDTO);
        return getDataTable(list);
    }

    /**
     * 查询商品规格名称信息下拉选列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:list')")
    @GetMapping("/dropDownList/{specificationId}")
    public AjaxResult dropDownList(@PathVariable("specificationId") Long specificationId )
    {
        List<ShopFlavour> shopFlavourList = shopFlavourService.queryShopFlavourList(specificationId);
        return success(shopFlavourList);
    }

    /**
     * 导出商品口味列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:export')")
    @Log(title = "商品口味", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopFlavourDTO shopFlavourDTO)
    {
        List<ShopFlavourVO> list = shopFlavourService.selectShopFlavourList(shopFlavourDTO);
        ExcelUtil<ShopFlavourVO> util = new ExcelUtil<ShopFlavourVO>(ShopFlavourVO.class);
        util.exportExcel(response, list, "商品口味数据");
    }

    /**
     * 获取商品口味详细信息
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:query')")
    @GetMapping(value = "/{flavourId}")
    public AjaxResult getInfo(@PathVariable("flavourId") Long flavourId)
    {
        return success(shopFlavourService.selectShopFlavourByFlavourId(flavourId));
    }

    /**
     * 修改商品回显
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:query')")
    @PostMapping(value = "/{echoShop}")
    public AjaxResult getEcho(@RequestBody ShopFlavourDTO shopFlavourDTO){
        List<ShopFlavourVO> shopFlavourVOList = shopFlavourService.selectShopFlavourList(shopFlavourDTO);
        ShopFlavourVO shopFlavourVO = shopFlavourVOList.get(0);
        return success(shopFlavourVO);

    }

    /**
     * 新增商品口味
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:add')")
    @Log(title = "新增商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopFlavourDTO shopFlavourDTO)
    {
        return toAjax(shopFlavourService.insertShopFlavour(shopFlavourDTO));
    }

    /**
     * 关联商品口味
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:add')")
    @Log(title = "关联商品规格", businessType = BusinessType.INSERT)
    @PostMapping("/associationShopFlavour")
    public AjaxResult associationShopFlavour(@RequestBody ShopFlavourDTO shopFlavourDTO)
    {
        shopFlavourService.associationShopFlavour(shopFlavourDTO);
        return success();
    }


    /**
     * 修改商品口味
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:edit')")
    @Log(title = "商品口味", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/editShopFlavour",method = RequestMethod.POST)
    public AjaxResult edit(@RequestBody ShopFlavourDTO shopFlavourDTO)
    {
        return toAjax(shopFlavourService.updateShopFlavour(shopFlavourDTO));
    }

    /**
     * 删除商品口味
     */
    @PreAuthorize("@ss.hasPermi('orderEat:flavour:remove')")
    @Log(title = "商品口味", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/deleteShopFlavour",method = RequestMethod.POST)
    public AjaxResult remove(@RequestBody Map<String,Object> idMap)
    {
        List<Integer> delIds = (List<Integer>) idMap.get("ids");
        return toAjax(shopFlavourService.deleteShopFlavourByFlavourIds(delIds));
    }
}
